import http from '@/utils/request'
import { setTokens,removeTokens } from '@/utils'
/* 
 发送短信验证码
 @param {string} mobile 手机号码
 @returns thunk
*/
export const sendValidationCode = (mobile) => {
    // 请求到的验证码不需要存储到redux中 所以不需要dispatch
    return async () => {
        const res = await http.get(`sms/codes/${mobile}`)
        // console.log(res, 'res验证码')
    }
}


/* 
登录
*/

export const login = (params) => {
    // console.log(params,'params')
    return async (dispatch) => {
        const res = await http.post(`authorizations`, params)
        // console.log(res,'res')
        const tokenInfo = res.data
        // 将token 存储到redux中
        dispatch(saveToken(tokenInfo))
        // 存储到本地
        setTokens(tokenInfo)
    }
}


/*
  将 Token 信息保存到 Redux 中
*/
export const saveToken = (tokenInfo) => {
    return {
        type: 'login/token',
        payload: tokenInfo
    }
}


/* 
退出登录
*/
export const logout = (tokenInfo) => {
    return (dispatch) =>{
        // 清空本地
        removeTokens()
        // 清空redux
        dispatch({
            type:'login/logout'
        })
    }
}